package xyz.zyl2020.juc.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author ZhuYinglong
 * @date 2020/10/8 0008
 */
public class ThreadDemo6 {

    public static void main(String[] args) throws InterruptedException {
        ThreadDemo6 threadDemo6 = new ThreadDemo6();

        new Thread(() -> threadDemo6.lock(), "myThread1").start();
        TimeUnit.SECONDS.sleep(1);

        Thread myThread2 = new Thread(() -> threadDemo6.lock(), "myThread2");
        myThread2.start();

        TimeUnit.SECONDS.sleep(1);
        // 在获取锁之前，该线程进入BLOCKED状态
        System.out.println(myThread2.getState());

    }

    public synchronized void lock() {
        System.out.println(Thread.currentThread().getName() + "-->获取锁");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "-->释放锁");
    }
}
